package com.yidu.qianfeng.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yidu.qianfeng.common.bean.transport.TransportProtocol;
import com.yidu.qianfeng.common.bean.waybill.Waybill;
import com.yidu.qianfeng.common.bean.waybill.WaybillCondition;
import com.yidu.qianfeng.common.util.LayuiJsonWrap;
import com.yidu.qianfeng.service.WaybillService;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author: TangSheng
 * @Description: 运单视图层
 * @Date Created in 16:55 2021/3/16
 * @since 1.0
 */
@Controller("waybillController")
public class WaybillController {

    //注入运单业务实现
    @Resource(name = "waybillServiceImpl")
    private WaybillService waybillService;


    /**
     *  新增运单请求
     * @param waybill 运单实体
     * @param request 请求对象
     * @return 新增运单结果
     */
    @RequestMapping("/addWaybill")
    public @ResponseBody String addWaybill(@RequestBody Waybill waybill, HttpServletRequest request){
        //调用业务层新增运单方法
        return waybillService.addNewWaybill(waybill,UserController.getLoginEmpInfo(request));
    }

    /**
     *  根据托运单号列表取消运单
     * @param waybillConsignNumS 取消的托运单号列表
     * @return 取消运单结果
     */
    @RequestMapping("/cancelWaybillByConsignNumS")
    public @ResponseBody String cancelWaybillByConsignNumS(@RequestBody String[] waybillConsignNumS){
        //调用业务层取消运单方法
        return waybillService.deleteWaybillByConsignNumS(waybillConsignNumS);
    }


    /**
     * 修改运单
     * @param waybill 带托运单号的运单实体
     * @return 修改结果
     */
    @RequestMapping("/updateWaybill")
    public @ResponseBody String updateWaybill(@RequestBody Waybill waybill){
        //调用业务层修改运单方法
        return waybillService.updateWaybill(waybill);
    }

    /**
     * 查询运单根据托运单号
     * @param waybillConsignNum 托运单号
     * @return 托运单号对应的运单
     */
    @RequestMapping("/queryWaybillById")
    public @ResponseBody Waybill queryWaybillById(@RequestParam String waybillConsignNum){
        //调用业务层方法根据托运单号查询运单
        Waybill waybill = waybillService.queryWaybillById(waybillConsignNum);
        return waybill;
    }

    /**
     * 分页查询运单数据
     * @param page 当前页码数
     * @param limit 显示条目数
     * @return layui格式数据包装类
     */
    @RequestMapping("/queryWaybillLimitLayui")
    public @ResponseBody
    LayuiJsonWrap queryWaybillLimitLayui(@Param("page") int page, @Param("limit") int limit){
        //调用业务层分页查询方法
        return waybillService.queryWaybillLimitLayui(page, limit);
    }

    /**
     * 根据条件查询运单数据并分页
     * @param waybillCondition 查询条件包装类
     * @param trackTypes 跟踪类型数组
     * @return layui格式数据包装类
     */
    @RequestMapping("/queryWaybillConditionLayui")
    public @ResponseBody LayuiJsonWrap queryWaybillConditionLayui(@RequestParam("waybillCondition" ) String waybillCondition,@RequestParam("trackTypes") String trackTypes){
        //查询条件包装类
        WaybillCondition condition = null;
        try {
            //jackson转换json字符串为condition
            condition = new ObjectMapper().readValue(waybillCondition, WaybillCondition.class);

            //转换跟踪类型数组为集合
            List<Integer> trackTypeList = new ObjectMapper().readValue(trackTypes, new TypeReference<List<Integer>>(){});
            //查询条件设置跟踪列表
            condition.setTrackTypes(trackTypeList);

        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        //调用业务层根据条件查询的方法
        return waybillService.queryWaybillConditionLimitLayui(condition);
    }

    /**
     * 查询司机正在运输的运输协议
     * @param carNumber 司机车号
     * @return 运输协议，只有出发站和到达地
     */
    @RequestMapping("/queryDriverNowRoute")
    public @ResponseBody TransportProtocol queryDriverNowRoute(String carNumber){
        TransportProtocol trans = waybillService.queryDriverNowRoute(carNumber);
        if (trans == null){
            trans = new TransportProtocol();
        }
        return trans;
    }


}
